﻿using AutoMapper;
using MediatR;
using Traceability.Doamin;
using Traceability.ErrorCode;
using Traceability.Infratruvture.Interfares;
using Traceability.Read.Api.Application.Command;
using Traceability.Read.Api.Dto;

namespace Traceability.Read.Api.Application.CommandHander
{
    public class GetSlaughterHander : IRequestHandler<GetSlaughterCommand, ApiResult<List<SlaughterDto>>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRequsitory<Slaughter> baseRequsitory;

        public GetSlaughterHander(IMapper mapper, IBaseRequsitory<Slaughter> baseRequsitory)
        {
            this.mapper = mapper;
            this.baseRequsitory = baseRequsitory;
        }

        /// <summary>
        /// 显示屠宰信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<List<SlaughterDto>>> Handle(GetSlaughterCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<SlaughterDto>> result = new ApiResult<List<SlaughterDto>>();

            result.Code = ApiEnum.显示成功;
            result.Msg = ApiEnum.显示成功.ToString();

            var list = baseRequsitory.GetAll().Where(x => x.CommodityId == request.CommodityId);

            result.Data = mapper.Map<List<SlaughterDto>>(list.ToList());

            return Task.FromResult(result);
        }
    }
}
